package com.foreknow.dao.impl;

import com.foreknow.dao.AdminDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Admin;

import java.sql.Connection;
import java.util.List;

public class AdminDaoImpl extends BaseDao implements AdminDao {

    @Override
    public Admin isLogin(String username, String password) {
        String sql = "select * from admin where username=? and password=?";
        //1. 连接数据库
        //DBManager dbManager = DBManager.getInstance();
        //Connection conn = dbManager.getConnection();
        //2. 获取到JdbcTemplate模板类的对象
        //JdbcTemplate jt = JdbcTemplate.getInstance();
        //3. 将DBManager中获取到的连接传递给JdbcTemplate中的Connection
        //jt.setConnection(conn);
        //4. 调用JdbcTemplate中查询的方法
        //MappingFactory mappingFactory = MappingFactory.getInstance();//获取工厂类的实例
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.ADMIN_MAPPING); //从工厂容器中获取到xxxMapping对象
        List<Object> list = jt.query(sql,mapping,username,password);
        if(list.size() > 0) {
            return (Admin) list.get(0);
        }
        return null;
    }
    @Override
    public int saveAdmin(Admin admin) {
        return 0;
    }

    @Override
    public Admin getQueryById(int id) {
        return null;
    }

    @Override
    public boolean deleteById(int id) {
        return false;
    }

    @Override
    public int updateAdmin(Admin admin) {
        return 0;
    }

    @Override
    public List<Admin> getQueryAll() {
        return null;
    }



    public static void main(String[] args) {
        AdminDao adminDao = new AdminDaoImpl();
        Admin admin = adminDao.isLogin("fender","123456");
        System.out.println(admin.getUsername()+"-------"+admin.getPassword());
    }
}
